package com.zzz.robot201.service.api;


import com.zzz.robot201.service.client.ResponseMsg;
import com.zzz.robot201.service.client.SendMsg;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Date;

public class RobotUdpUtil {
    DatagramSocket ds = null;

    private static String robotHost = "192.168.2.153";
    private static int robotPort = 42785;

    public static void main(String[] args) {
        RobotUdpUtil udp = new RobotUdpUtil();
        int test = 1;
        if(test==0){// 发起通信
            long[] unsigned_long_long = new long[]{0,0};//时间，暂时都设为0;
            float[] float3 = new float[]{-1.1f,0.2f,0.3f};// 顺序为转向速度，y方向速度，x方向速度
            float[] float2 = new float[]{0f,0f};// 固定 0,0
            byte unsigned_char_byte15_0= 7;// byte[0] = 当前步态序号
            byte unsigned_char_byte15_4= 3;// byte[4] = 当前状态序号
            SendMsg sendMsg = new SendMsg(robotHost,robotPort,unsigned_long_long[0],unsigned_long_long[1])
                    .setFloat3(float3).setFloat2(float2)
                    .setUnsigned_char_byte15_0(unsigned_char_byte15_0)
                    .setUnsigned_char_byte15_4(unsigned_char_byte15_4)
                    .checkBuf();
            udp.sendMsg2(sendMsg);
        }else {
            // 接收通信
            udp.receiveMsg("00000001",43795);
        }
    }
    public boolean sendMsg2(SendMsg sendMsg){
        boolean result= false;
        try {
            Socket socket = new Socket(sendMsg.getServerHost(), sendMsg.getServerPort(),false);// stream>true:tcp,false:udp
            socket.getOutputStream().write(new SendMsg().getbuf());
            result= true;
            System.out.println("发送："+result);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(ds!=null){
                ds.close();
            }
            return result;
        }
    }

    public boolean sendMsg(SendMsg sendMsg){
        boolean result= false;
        try {
            ds = new DatagramSocket();
            byte[] data = sendMsg.getbuf();
            DatagramPacket dp = new DatagramPacket(data, data.length,
                    InetAddress.getByName(sendMsg.getServerHost()),
                    sendMsg.getServerPort());
            ds.send(dp);
            result= true;
            System.out.println("发送："+result);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(ds!=null){
                ds.close();
            }
            return result;
        }
    }

    public void receiveMsg(String deviceCode,int localPort){
        boolean result= false;
        try {
            ds = new DatagramSocket(localPort);
            while (true){
                byte[] buffer = new byte[260];
                DatagramPacket dp = new DatagramPacket(buffer, buffer.length);
                ds.receive(dp);
                buffer = dp.getData();
                ResponseMsg responseMsg = ResponseMsg.getResponseMsg(buffer);

                InetAddress address = dp.getAddress();
                int port = dp.getPort();
                responseMsg.setHostIp(address.getHostAddress());
                responseMsg.setHostPort(port);
                responseMsg.setNowtime(new Date().getTime());
                responseMsg.setDeviceCode(deviceCode);
                System.out.println(responseMsg);

                //JavaBean
                System.out.println(String.format("%s,%s,%d",
                        address.getHostAddress(),
                        address.getCanonicalHostName(),
                        port));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(ds!=null){
                ds.close();
            }
        }
    }
}
